﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DevExpress.XtraBars;
using DataCentral.BusinessDataObjects;
using DevExpress.XtraNavBar;
using DataCentral.Dataset;

namespace DataCentral.UsecaseControllers
{
    public partial class MainUsecase
    {
        private bool IsExistMenuAccessRight(string barName, DataTable tblAccessRight)
        {
            for (int i = 0; i < tblAccessRight.Rows.Count; i++)
                if (tblAccessRight.Rows[i]["MenuItem"].ToString().Trim().Equals(barName))
                    return true;
            return false;
        }

        private bool IsExistNavbarItem(string barName, DataTable tblAccessRight)
        {
            for (int i = 0; i < tblAccessRight.Rows.Count; i++)
                if (tblAccessRight.Rows[i]["NavBarItem"].ToString().Trim().Equals(barName))
                    return true;
            return false;
        }

        public void ProcessAccessRight(BarManager barManager, NavBarControl navBarControl, UserBO userBO)
        {           
            DataTable tblAccessRight = userBO.GetAccessRightList(userBO.RoleID);
            for (int i = 0; i < barManager.Items.Count; i++)
            {
                if (barManager.Items[i].Name == "iPaintStyle" || barManager.Items[i].Caption.StartsWith("Giao diện")||
                    barManager.Items[i].Description == "StatusBar")
                {
                    barManager.Items[i].Enabled = true;
                }
                else
                {
                    barManager.Items[i].Enabled = IsExistMenuAccessRight(barManager.Items[i].Name, tblAccessRight);
                }
            }

            for (int j = 0; j < navBarControl.Items.Count; j++)
            {
                navBarControl.Items[j].Enabled = IsExistNavbarItem(navBarControl.Items[j].Name, tblAccessRight);
            }
        }

        private decimal GetDecimalValue(decimal? dValue)
        {
            if (dValue.HasValue)
                return dValue.Value;
            else return 0;
        }
    }
}
